package br.com.JZSolution.domineduplex.gui.paineltransferencia;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.io.File;

import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;

import br.com.JZSolution.domineduplex.DomineDuplex;
import br.com.JZSolution.domineduplex.swing.JProgressBarJZ;

public class FileProgress extends JPanel{
	
	private JProgressBarJZ progressBar;
	private File file;
	private long relativo = 1;
	private int ratio = 0;
	private JLabel labelConcluido;
	
	/**
	 * Transferencia de UPLOAD
	 * @param file
	 */
	public FileProgress(File file) {
		super(new BorderLayout(0,0));
		this.setBackground(Color.WHITE);
		this.file = file;
		relativo = file.length()/100; 
		init(1);
	}
	
	/**
	 * Transferencia de DOWNLOAD
	 * @param tamanho
	 */
	public FileProgress(long tamanho) {
		super(new BorderLayout(0,0));
		relativo = tamanho/100; 
		init(2);
	}
	
	private void init(int tipo){
		
		if(relativo == 0){
			relativo = 1;
		}
		//criando de acordo com o tipo
		if (tipo == 1 ){
			progressBar = new JProgressBarJZ(10,2,Color.BLUE,Color.BLACK);
		}else{
			progressBar = new JProgressBarJZ(10,2);
		}
		
		labelConcluido = new JLabel("0 %");
		labelConcluido.setAlignmentX(CENTER_ALIGNMENT);
		this.add(progressBar,BorderLayout.CENTER);
		progressBar.setPreferredSize(new Dimension(DomineDuplex.getInstance().getMainframe().getTransferPanel().getTransferTable().getColumnModel().getColumn(1).getWidth()-10,50));
		
	}
	public void atualizarProgress(int totalLido){
		ratio = (int) (totalLido / relativo);
		progressBar.setPorcentagem(totalLido / relativo);
		labelConcluido.setText(" "+ratio+" %");
	}
	
	public long getRelativo() {
		return relativo;
	}
	
	public JProgressBarJZ getComponenteVisual(){
		return progressBar;
	}
	public File getFile() {
		return file;
	}

	public JLabel getLabelConcluido() {
		return labelConcluido;
	}
}
